import turtle
from turtle import *
import random


def drawRoad():
    """
    画出赛场道路
    :return:
    """
    turtle.tracer(0)
    turtle.hideturtle()
    pencolor("black")
    penup()
    setposition(-150, 300)
    pendown()
    setposition(-150, -300)
    penup()
    setposition(150, 300)
    pendown()
    setposition(150, -300)

    for i in range(-300, 300, 60):
        penup()
        setposition(0, i)
        pendown()
        setposition(0, i + 10)
        penup()
        setposition(0, i + 40)
        pendown()
        setposition(0, i + 60)


def drawEnemyCar(enemy_cars):
    """
    画敌方汽车
    :param enemy_cars:
    :return:
    """
    for enemy_car in enemy_cars:
        enemy_x = enemy_car[0]
        enemy_y = enemy_car[1]
        pencolor("black")
        fillcolor("red")
        penup()
        setposition(enemy_x - 10, enemy_y)
        begin_fill()
        pendown()
        setposition(enemy_x + 10, enemy_y)
        setposition(enemy_x, enemy_y - 20)
        setposition(enemy_x - 10, enemy_y)
        end_fill()


def moveEnemyCar(car_speed):
    global enemy_car_pos
    for enemy_car in enemy_car_pos:
        enemy_y = enemy_car[1]
        enemy_car[1] = enemy_y - car_speed


def removeEnemyCar():
    global enemy_car_pos, score
    for enemy_car in enemy_car_pos:
        enemy_y = enemy_car[1]
        if enemy_y < -300:
            enemy_car_pos.remove(enemy_car)
            score = score + 10


def carCollision():
    car_x = car_pos[0]
    car_y = car_pos[1]
    for enemy_car in enemy_car_pos:
        enemy_car_x = enemy_car[0]
        enemy_car_y = enemy_car[1]
        if enemy_car_x == car_x and \
                abs(enemy_car_y - car_y) < 20:
            return True
    return False


def genEnemyCar():
    global enemy_car_pos
    if len(enemy_car_pos) > 5:
        return
    if len(enemy_car_pos) > 0:
        last_enemy = enemy_car_pos[len(enemy_car_pos) - 1]
        if last_enemy[1] > 100:
            return

    enemy_flag = random.randint(0, 1)
    if enemy_flag == 1:
        gen_enemy_pos = [75, 300]
    else:
        gen_enemy_pos = [-75, 300]
    enemy_car_pos.append(gen_enemy_pos)


def drawMyCar(car_pos):
    """
    画我方汽车
    :param carPos:
    :return:
    """
    pencolor("black")
    penup()
    car_x = car_pos[0]
    car_y = car_pos[1]
    setposition(car_x - 10, car_y)
    pendown()
    setposition(car_x + 10, car_y)
    setposition(car_x, car_y + 20)
    setposition(car_x - 10, car_y)


def drawScore():
    global score
    penup()
    setposition(280, 300)
    pencolor("black")
    pendown()
    write_str = "分数：" + str(score)
    write(write_str, font=("Arial", 16))


def tick():
    """
    刷新
    :return:
    """
    global car_pos, enemy_car_pos
    try:
        removeEnemyCar()
        genEnemyCar()
        moveEnemyCar(car_speed)
        turtle.clear()

        drawRoad()
        drawMyCar(car_pos)
        drawEnemyCar(enemy_car_pos)
        drawScore()

        if carCollision():
            penup()
            setposition(car_pos[0], car_pos[1] - 10)
            pendown()
            begin_fill()
            fillcolor("red")
            circle(20)
            end_fill()
            # 碰撞后，显示"按空格重新开始"
            penup()
            setposition(-90,0)
            pencolor("red")
            pendown()
            write("按空格键重新开始！", font=("Arial", 24))
            turtle.update()
            return
        else:
            turtle.update()
            ontimer(tick, 50)
    except Terminator:
        pass  # turtledemo user pressed STOP


def keyLeft():
    """
    按左键，
    :return:
    """
    global car_pos
    if car_pos[0] != -75:
        car_pos[0] = -75


def keyRight():
    """
    按右键，
    :return:
    """
    global car_pos
    if car_pos[0] != 75:
        car_pos[0] = 75


def keySpace():
    global car_pos, enemy_car_pos,score
    score = 0
    car_pos = [-75, -300]
    enemy_car_pos = []
    tick()


car_speed = 15
score = 0
car_pos = [-75, -300]
enemy_car_pos = []
if __name__ == '__main__':
    turtle.screensize(600, 600)
    turtle.listen()
    turtle.onkeypress(keyLeft, "Left")
    turtle.onkeypress(keyRight, "Right")
    turtle.onkeypress(keySpace, "space")
    tick()
    mainloop()
